/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cash_access;

import mware_lib.IBaseSkeleton;

/**
 * @author Gortex
 */
public class TransactionSkeleton implements IBaseSkeleton {

    TransactionImplBase original;

    protected TransactionSkeleton(TransactionImplBase original) {
        this.original = original;
        System.out.println("TransactionSkeleton gestartet");
    }

    /**
     * Diese methode kriegt den kodierten String(nach request reply protokoll) prüft welche methode genau auf dem originalobjekt
     *aufgerufen werden soll, prüft ob die parametertypen stimmen und ruft dann die Methode auf dem original auf.
     *sollte eine Exception auftreten wird diese wieder mit dem request-reply protokoll an das Proxyobjekt
     *zurückgeleitet und dort geworfen.
     */
    @Override
    public synchronized String remoteInvoke(String message) {

        try {
            String[] messageAry = message.split(";");
            if (messageAry[0].split(":")[1].equals("deposit")) {
                String typ1 = messageAry[1].split(":")[1];
                String typ2 = messageAry[3].split(":")[1];
                if (typ1.equals("String") && typ2.equals("double")) {
                    String param1 = messageAry[2].split(":")[1];
                    double param2 = Double.parseDouble(messageAry[4].split(":")[1]);
                    String returnval = "exception:none;returntyp:void;returnvalue:void";
                    original.deposit(param1, param2);
                    return returnval;
                } else {
                    String returnval = "exception:exception;returntyp:RuntimeException;returnvalue:typemismatch";
                    return returnval;
                }

            } else if (messageAry[0].split(":")[1].equals("withdraw")) {
                String typ1 = messageAry[1].split(":")[1];
                String typ2 = messageAry[3].split(":")[1];

                if (typ1.equals("String") && typ2.equals("double")) {
                    String param1 = messageAry[2].split(":")[1];
                    double param2 = Double.parseDouble(messageAry[4].split(":")[1]);

                    try {
                        original.withdraw(param1, param2);
                        String returnval = "exception:none;returntyp:void;returnvalue:void";
                        return returnval;
                    } catch (OverdraftException ex) {
                        String returnval = "exception:exception;returntyp:OverdraftException;returnvalue:" + ex.getMessage();
                        return returnval;
                    }

                } else {
                    String returnval = "exception:exception;returntyp:RuntimeException;returnvalue:typemismatch";
                    return returnval;
                }

            } else if (messageAry[0].split(":")[1].equals("getBalance")) {
                String typ1 = messageAry[1].split(":")[1];
                if (typ1.equals("String")) {
                    String param1 = messageAry[2].split(":")[1];
                    String returnval = "exception:none;returntyp:double;returnvalue:" + original.getBalance(param1);
                    return returnval;
                } else {
                    String returnval = "exception:exception;returntyp:RuntimeException;returnvalue:typemismatch";
                    return returnval;
                }

            } else {
                String returnval = "exception:exception;returntyp:RuntimeException;returnvalue:method not found";
                return returnval;
            }
        } catch (Exception e) {
            String returnval = "exception:exception;returntyp:RuntimeException;returnvalue:" + e.getMessage();
            return returnval;
        }
    }
}
